//
//  TOToolBar.m
//  X-Tribe
//
//  Created by Miss.Ou on 16/4/9.
//  Copyright © 2016年 Miss.Ou. All rights reserved.
//

#import "TOToolBar.h"

@interface TOToolBar()
@property(nonatomic,weak)UIButton *smileBtn;

@end

@implementation TOToolBar


-(id)initWithFrame:(CGRect)frame
{
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"compose_toolbar_background"]];
        
        // 添加所有的子控件
        
        [self addButtonWithIcon:@"compose_camerabutton_background" highIcon:@"compose_camerabutton_background_highlighted" tag:TOToolbarButtonTypeCamera];
        
        [self addButtonWithIcon:@"compose_toolbar_picture" highIcon:@"compose_toolbar_picture_highlighted" tag:TOToolbarButtonTypePicture];
        
        [self addButtonWithIcon:@"compose_mentionbutton_background" highIcon:@"compose_mentionbutton_background_highlighted" tag:TOToolbarButtonTypeMention];
        
        [self addButtonWithIcon:@"compose_trendbutton_background" highIcon:@"compose_trendbutton_background_highlighted" tag:TOToolbarButtonTypeTrend];
        
        self.smileBtn = [self addButtonWithIcon:@"compose_emoticonbutton_background" highIcon:@"compose_emoticonbutton_background_highlighted" tag:TOToolbarButtonTypeEmotion];
        
        
    }
    return self;
    
}


-(void)setShowEmototionBtn:(BOOL)showEmototionBtn
{
    _showEmototionBtn = showEmototionBtn;
    if (showEmototionBtn) {
        [self.smileBtn setImage:[UIImage imageWithName:@"compose_emoticonbutton_background"] forState:UIControlStateNormal];
        [self.smileBtn setImage:[UIImage imageWithName:@"compose_emoticonbutton_background_highlighted"] forState:UIControlStateHighlighted];
    }
    else
    {
        [self.smileBtn setImage:[UIImage imageWithName:@"compose_keyboardbutton_background"] forState:UIControlStateNormal];
        [self.smileBtn setImage:[UIImage imageWithName:@"compose_keyboardbutton_background_highlighted"] forState:UIControlStateHighlighted];
        
        
    }
}

-(UIButton *)addButtonWithIcon:(NSString *)icon highIcon:(NSString *)highIcon tag:(TOToolbarButtonType)tag
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tag = tag;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [button setImage:[UIImage imageWithName:icon] forState:UIControlStateNormal];
    [button setImage:[UIImage imageWithName:highIcon] forState:UIControlStateHighlighted];
    [self addSubview:button];
    return button;
    
}
/**
 *  监听按钮点击
 */
- (void)buttonClick:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(composeTool:didClickedButton:)]) {
        [self.delegate composeTool:self didClickedButton:(int)button.tag];
        
    }
}

//重新设置frame
-(void)layoutSubviews
{
    [super layoutSubviews];
    long count = self.subviews.count;
    CGFloat buttonW = self.width / count;
    CGFloat buttonH = self.height;
    for (int i = 0; i<count; i++) {
        UIButton *button = self.subviews[i];
        button.y = 0;
        button.width = buttonW;
        button.height = buttonH;
        button.x = i * buttonW;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
